package com.yellowpage.ca.service;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.yellowpage.white.common.PageRender;
import com.yellowpage.white.dao.QueryAssistantSQL;
import com.yellowpage.white.service.BaseServiceImpl;

import com.yellowpage.ca.data.User;
import com.yellowpage.ca.dao.UserDao;

public class UserServiceImpl extends BaseServiceImpl implements UserService {
	private UserDao userDao;

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	/* white generate Method */
	public User getUser(Long id) {
		return userDao.get(id);
	}

	public List<User> getAllUser() {
		return userDao.getAll();
	}

	public List<Map> queryUserByPage(Map queryMap, PageRender pageRender) {
		QueryAssistantSQL sqlAssistant = baseDao
				.newQueryAssistantSQL(
						"select u.user_id, u.name, u.display_name, u.status, u.email,"
								+ "       date_format(u.last_login_time,'%Y-%m-%d %T') as last_login_time,"
								+ "       (select display_name from CA_USER where user_id = u.last_update_user) as last_update_user, "
								+ "       date_format(u.last_update_time,'%Y-%m-%d %T') as last_update_time, "
								+ "       (select display_name from CA_USER where user_id = u.create_user) as create_user,"
								+ "       date_format(u.create_time,'%Y-%m-%d %T') as create_time "
								+ "  from CA_USER u where 1=1 ")
				.addCondition(" and EMAIL like :email ")
				.addCondition(" and date_format(u.LAST_LOGIN_TIME,'%Y-%m-%d') = :lastLoginTime  ")
				.addCondition(" and NAME like :name ")
				.addCondition(" and DISPLAY_NAME like :displayName ")
				.addCondition(" and STATUS = :status ");

		List<Map> resultList = sqlAssistant.queryByPage(
				baseDao.getConnection(), queryMap, pageRender);

		return resultList;
	}

	public void saveUser(User user) {
		userDao.save(user);
	}

	public void updateUser(User user) {
		userDao.update(user);
	}

	public void delete(List deleteList) {
		userDao.remove(deleteList);
	}

	public User getUserByName(String name) {
		return userDao.getUserByName(name);
	}

	public List<Map<String, Object>> getFunctionsByUser(Long userId) {
		return userDao.getFunctionsByUser(userId);
	}

	@Override
	public Set<String> getFunctionCodesByUser(Long userId) {
		Set<String> set = new HashSet<String>();
		List<Map<String, Object>> list = getFunctionsByUser(userId);
		for (Map<String, Object> map : list) {
			set.add((String) map.get("FUNC_CODE"));
		}
		return set;
	}

}